package edu.hawaii.myisern.action;

import com.meterware.httpunit.TableCell;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;
import edu.hawaii.myisern.model.Log;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

/**
 * Tests operation of the MyIsern collaboration web pages.
 * 
 * @author Randy Cox
 */
public class TestCollaborationActionBeans {

  /** Get the test host. */
  private String Url = System.getProperty("test.host") + "myisern-tan";
  private String username = "test";
  private String password = "test";
  private String success = "You have logged in successfully!";

  /**
   * Test the main menu new collaboration button.
   * 
   * @throws Exception If problem occurs.
   */
  @Test
  public void testCollaboration() throws Exception {
    Log.enter("TestCollaborationActionBeans.testCollaboration() start.");
    WebConversation conversation = new WebConversation();

    // Get login page and check title
    Log.enter("Connecting to Url: " + Url);
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking index.jsp retrieval", "MyIsern 2.0", response.getTitle());

    // Push login button
    Log.enter("Pushing login button.");
    WebForm form = response.getFormWithID("LoginForm");
    WebRequest request = form.getRequest();
    request.setParameter("username", this.username);
    request.setParameter("password", this.password);
    response = conversation.getResponse(form.getRequest());
    String text = response.getText();
    assertTrue("Checking login", text.contains(success));

    // Click on select collaboration.
    WebLink link = response.getLinkWithID("SelectCollaborationLink");
    response = conversation.getResponse(link.getRequest());
    text = response.getText();
    assertTrue("Checking select collaboration", text.contains("List of ISERN Collaborations:"));

    // Clear collaborations.
    link = response.getLinkWithID("ClearCollaborationLink");
    response = conversation.getResponse(link.getRequest());
    WebTable table = response.getTableWithID("SelectCollaborationTable");
    Integer expected = 1;
    Integer count = table.getRowCount();
    assertEquals("Collaborations should have only header row", expected, count);

    // Click on new collaboration link.
    Log.enter("Click on new collaboration link.");
    link = response.getLinkWithID("NewCollaborationLink");
    response = conversation.getResponse(link.getRequest());
    text = response.getText();
    assertTrue("Checking new Collaboration", text.contains("Edit ISERN Collaboration:"));

    // Click on save.
    Log.enter("Click on save.");
    form = response.getFormWithID("EditCollaborationForm");
    form.setParameter("name", "Test Collaboration");
    form.setParameter("description", "Test Description");
    // String[] list = { "Test Organization" };
    // form.setParameter("newCollaboratingOrganization", list);
    form.setParameter("newCollaborationType", "Type1");
    form.setParameter("newOutcomeType", "Outcome1");
    // form.setParameter("newYear", "2001");
    response = conversation.getResponse(form.getRequest("save"));

    // Check if new name is in select list and click it.
    Log.enter("Check if new name is in select list and click it.");
    table = response.getTableWithID("SelectCollaborationTable");
    TableCell cell = table.getTableCell(1, 0);
    assertNotNull("Check if cell exisits", cell);
    String contents = cell.getText();
    Log.enter("TestCollaborationActionBeans cell contents = " + contents);
    link = cell.getLinkWith("Test");
    assertNotNull("Check if link in cell exisits", link);

    // Check that we are back at Edit screen, and check the fields for
    // data.
    response = conversation.getResponse(link.getRequest());
    form = response.getFormWithID("EditCollaborationForm");
    assertEquals("Check field.", "Test Collaboration", form.getParameterValue("name"));
    assertEquals("Check field.", "Test Description", form.getParameterValue("description"));
    Log.enter("TestCollaborationActionBeans.testMainMenNewCollaborationButton() finish.");

    // Cancel
    Log.enter("Click on cancel.");
    form = response.getFormWithID("EditCollaborationForm");
    response = conversation.getResponse(form.getRequest("cancel"));
  }

}
